package com.zucai365.football.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wcj.common.service.impl.WcjServiceImpl;
import com.zucai365.football.bean.CurrentFootballTeamBean;
import com.zucai365.football.bean.FootballTeamBean;
import com.zucai365.football.bean.GameFireBean;
import com.zucai365.football.bean.LianSaiBean;
import com.zucai365.football.bean.LianSaiYearBean;
import com.zucai365.football.bean.TeamDZBean;
import com.zucai365.football.dao.CurrentFootballTeamDao;
import com.zucai365.football.service.CurrentFootballTeamService;
import com.zucai365.football.service.GameFireService;
import com.zucai365.football.service.TeamDZService;
import com.zucai365.football.service.TeamPlayerService;
import com.zucai365.football.utils.ZuCaiUtils;

@Service("currentFootballTeamService")
public class CurrentFootballTeamServiceImpl extends WcjServiceImpl<CurrentFootballTeamBean> implements CurrentFootballTeamService{
	
	@Autowired
	@Qualifier("currentFootballTeamDao")
	private CurrentFootballTeamDao currentFootballTeamDao;

	public CurrentFootballTeamDao getCurrentFootballTeamDao() {
		return currentFootballTeamDao;
	}

	public void setCurrentFootballTeamDao( CurrentFootballTeamDao currentFootballTeamDao ) {
		this.currentFootballTeamDao = currentFootballTeamDao;
	}

	@Override
	public void saveCurrentYearFootBallInfo(LianSaiBean lianSai,
			LianSaiYearBean lianSaiYear, List<FootballTeamBean> footballTeamList) {
		
		List<CurrentFootballTeamBean> currentFootballTeamList = new ArrayList<CurrentFootballTeamBean>();
		for( FootballTeamBean footballTeam : footballTeamList ){
			CurrentFootballTeamBean currentFootballTeam = this.getCurrentFootballTeam( lianSai.getId(), lianSaiYear.getId(), footballTeam.getId() );
			
			if( currentFootballTeam == null ){
				currentFootballTeam = new CurrentFootballTeamBean();
			}
			
			currentFootballTeam.setFootballTeam(footballTeam);
			currentFootballTeam.setLiansai( lianSai );
			currentFootballTeam.setLiansaiYear( lianSaiYear );
			currentFootballTeam.setFootballTeamName( footballTeam.getFootballTeamName() );
			
			currentFootballTeam.setUrlBet007( "http://info.bet007.com/cn/team/Summary.aspx?TeamID="+footballTeam.getBet007Id() );
			currentFootballTeam.setUrlPlayer( "http://info.bet007.com/jsData/teamInfo/teamDetail/tdl"+footballTeam.getBet007Id()+".js" );
			
			currentFootballTeamList.add( currentFootballTeam );
		}
		
		this.currentFootballTeamDao.saveAll( currentFootballTeamList );
		
		
		//todo 生成球队人员列表文件
		ZuCaiUtils.dowloadTeamPlayerJS(currentFootballTeamList);
	}

	@Override
	public CurrentFootballTeamBean getCurrentFootballTeam(Long lianSaiId,Long lianSaiYearId, Long footballTeamId) {

		DetachedCriteria criteria = DetachedCriteria.forClass( CurrentFootballTeamBean.class, "currentFootballTeam" );
		criteria.add( Restrictions.eq( "currentFootballTeam.liansaiYear.id", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "currentFootballTeam.liansai.id", lianSaiId ) );
		criteria.add( Restrictions.eq( "currentFootballTeam.footballTeam.id", footballTeamId ) );
		
		List<CurrentFootballTeamBean> list = this.currentFootballTeamDao.findByCriteria(criteria);
		if( list.size() > 1){
			throw new RuntimeException( "每个赛季球队记录插入错误！" );
		}
		
		if( list.isEmpty() ){
			return null;
		}
		
		return list.get(0);
	}

	@Override
	public List<CurrentFootballTeamBean> getCurrentFootballTeamList(
			Long lianSaiId, Long lianSaiYearId) {

		DetachedCriteria criteria = DetachedCriteria.forClass( CurrentFootballTeamBean.class, "currentFootballTeam" );
		
		if( lianSaiId !=null ){
			criteria.add( Restrictions.eq( "currentFootballTeam.liansai.id", lianSaiId ) );
		}
		
		if( lianSaiYearId !=null ){
			criteria.add( Restrictions.eq( "currentFootballTeam.liansaiYear.id", lianSaiYearId ) );
		}
		
		criteria.addOrder( Order.desc( "currentFootballTeam.sumJiFen" ) );
		List<CurrentFootballTeamBean> list = this.currentFootballTeamDao.findByCriteria(criteria);
		
		return list;
		
	}
	
	@Autowired
	@Qualifier("teamDZService")
	private TeamDZService teamDZService;
	
	public TeamDZService getTeamDZService() {
		return teamDZService;
	}

	public void setTeamDZService(TeamDZService teamDZService) {
		this.teamDZService = teamDZService;
	}

	
	@Override
	public void saveFootballTeamJiFen(Long lianSaiId, Long lianSaiYearId) {
		
//		//一个赛季的球队
//		List<CurrentFootballTeamBean> currentFootballTeamList = this.getCurrentFootballTeamList( lianSaiId, lianSaiYearId );
//		
//		//结束的比赛列表
//		List<TeamDZBean> teamDZList = this.teamDZService.getTeamDzList(lianSaiId, lianSaiYearId);
//		
//		for( CurrentFootballTeamBean currentFootballTeam : currentFootballTeamList ){
//			//球队名称
//			String teamName = currentFootballTeam.getFootballTeamName();
//			currentFootballTeam.setSumJiFen( 0 );
//			currentFootballTeam.setZcJiFen( 0 );
//			currentFootballTeam.setKcJiFen( 0 );
//			
//			for( TeamDZBean teamDZ : teamDZList){
//				//如果是主队
//				if( teamName.equals( teamDZ.getZdName() ) ){
//					currentFootballTeam.setSumJiFen( currentFootballTeam.getSumJiFen() + teamDZ.getZdJifen() );
//					currentFootballTeam.setZcJiFen( currentFootballTeam.getZcJiFen() + teamDZ.getZdJifen() );
//				}else if( teamName.equals( teamDZ.getKdName() )  ){ //如果客队
//					currentFootballTeam.setSumJiFen( currentFootballTeam.getSumJiFen() + teamDZ.getKdJifen() );
//					currentFootballTeam.setKcJiFen( currentFootballTeam.getKcJiFen() + teamDZ.getKdJifen() );
//				}else{
//					continue;
//				}
//				
//			}
//		}
//		
//		
//		this.currentFootballTeamDao.saveAll( currentFootballTeamList );
		
		this.currentFootballTeamDao.saveFootballTeamJiFen(lianSaiId, lianSaiYearId);
		
	}
	
	@Autowired
	@Qualifier("teamPlayerService")
	private TeamPlayerService teamPlayerService;

	public TeamPlayerService getTeamPlayerService() {
		return teamPlayerService;
	}

	public void setTeamPlayerService(TeamPlayerService teamPlayerService) {
		this.teamPlayerService = teamPlayerService;
	}

	@Override
	public void saveFootballTeamJinQiu( Long lianSaiId, Long lianSaiYearId ) {
		
//		//一个赛季的球队
//		List<CurrentFootballTeamBean> currentFootballTeamList = this.getCurrentFootballTeamList( lianSaiId, lianSaiYearId );
//		
//		//所有的进球
//		List<GameFireBean> gameFireList = this.gameFireService.searchAllJinQiu(lianSaiId, lianSaiYearId);
//		
//		for( CurrentFootballTeamBean currentFootballTeam : currentFootballTeamList ){
//			//球队名称
//			String teamName = currentFootballTeam.getFootballTeamName();
//			currentFootballTeam.setSumJinQiu( 0 );
//			currentFootballTeam.setZcJinQiu( 0 );
//			currentFootballTeam.setKcJinQiu( 0 );
//			
//			for( GameFireBean gameFire : gameFireList){
//				
//				//如果是同一只球队,判断是主队还是客队
//				if( teamName.equals( gameFire.getEventFootballTeamName() ) ){
//					//主队
//					if( gameFire.getEventFootballTeamName().equals( gameFire.getTeamDz().getZdName() ) ){
//						currentFootballTeam.setSumJinQiu( currentFootballTeam.getSumJinQiu() + 1 );
//						currentFootballTeam.setZcJinQiu( currentFootballTeam.getZcJinQiu() + 1 );
//					}else{
//						currentFootballTeam.setSumJinQiu( currentFootballTeam.getSumJinQiu() + 1 );
//						currentFootballTeam.setKcJinQiu( currentFootballTeam.getKcJinQiu() + 1 );
//					}
//				}
//				
//			}			
//		}
//		
//		this.currentFootballTeamDao.saveAll( currentFootballTeamList );
		
		//统计球队总进球数
		this.currentFootballTeamDao.saveFootballTeamJinQiu(lianSaiId, lianSaiYearId);
		
		//统计球员总进球数
		this.teamPlayerService.saveTeamPlayerJinQiu(lianSaiYearId, lianSaiId);
	}
	
}
